package com.ace3l.legal.gwt.homeportal.createlawfirm.client;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.FormPanel;
import com.gwtext.client.widgets.form.Label;
import com.gwtext.client.widgets.form.TextField;
import com.gwtext.client.widgets.form.ComboBox;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.data.Store;
import com.gwtext.client.data.SimpleStore;
import com.ace3l.legal.gwt.domainmodule.gwt.GWTLawFirm;
import com.ace3l.legal.gwt.domainmodule.gwt.GWTUser;
import com.ace3l.legal.gwt.domainmodule.client.UserService;
import com.ace3l.legal.gwt.domainmodule.client.UserServiceAsync;
import com.ace3l.legal.gwt.domainmodule.client.NavDirector;
import com.synesthesiaconsulting.gwt.resources.client.DirectorUnit;
import com.synesthesiaconsulting.gwt.resources.client.GWTUtils;
import com.synesthesiaconsulting.gwt.resources.client.MsgObjBundle;

/**
 * Created by IntelliJ IDEA.
 * User: ageer
 * Date: Jul 22, 2008
 * Time: 10:42:20 AM
 * To change this template use File | Settings | File Templates.
 */
public class LawFirmSignUpWidget extends Composite {

    public LawFirmSignUpWidget(final NavDirector pd) {
        Panel panel = new Panel();
        panel.setBorder(false);
        panel.setPaddings(15);

        Label spacer = new Label();
        spacer.setHeight(30);

        //create the form
        final FormPanel formPanel = new FormPanel();
        formPanel.setFrame(false);
        Label lTitle = new Label();
        lTitle.setHtml("New Law Firm Sign Up");
        formPanel.add(lTitle);
        formPanel.setPaddings(10);

        final TextField firmName = new TextField("Firm Name", "firmname", 200);
        firmName.setAllowBlank(false);
        firmName.setMaxLength(60);
        firmName.setMinLength(5);
        formPanel.add(firmName);

        formPanel.add(spacer);

        final TextField contactFirstName = new TextField("First Name", "firstname", 200);
        contactFirstName.setAllowBlank(false);
        contactFirstName.setMaxLength(30);
        contactFirstName.setMinLength(1);
        formPanel.add(contactFirstName);

        final TextField contactLastName = new TextField("Last Name", "lastname", 200);
        contactLastName.setAllowBlank(false);
        contactLastName.setMaxLength(30);
        contactLastName.setMinLength(1);
        formPanel.add(contactLastName);

        final TextField firmAddress1 = new TextField("Address 1", "address1", 300);
        firmAddress1.setAllowBlank(false);
        firmAddress1.setMaxLength(60);
        firmAddress1.setMinLength(3);
        formPanel.add(firmAddress1);

        final TextField firmAddress2 = new TextField("Address 2", "address2", 300);
        firmAddress2.setAllowBlank(true);
        firmAddress2.setMaxLength(60);
        formPanel.add(firmAddress2);

        final TextField firmCity = new TextField("City", "city", 150);
        firmCity.setAllowBlank(false);
        firmCity.setMaxLength(20);
        firmCity.setMinLength(1);
        formPanel.add(firmCity);

        final Store store = new SimpleStore(new String[]{"abbr", "state"}, GWTUtils.STATES_ARRAY);
        final ComboBox state = new ComboBox();
        store.load();
        state.setForceSelection(true);
        state.setMinChars(1);
        state.setFieldLabel("State");
        state.setStore(store);
        state.setDisplayField("state");
        state.setValueField("abbr");
        state.setMode(ComboBox.LOCAL);
        state.setEmptyText("Enter state");
        state.setTypeAhead(true);
        state.setSelectOnFocus(true);
        state.setWidth(200);
        formPanel.add(state);

        final TextField firmZip = new TextField("Zip", "zip", 100);
        firmZip.setAllowBlank(false);
        firmZip.setMaxLength(5);
        firmZip.setMinLength(5);
        firmZip.setRegex("^[0-9]{5}$");
        firmZip.setRegexText("Zip must be a 5 digit number.");
        formPanel.add(firmZip);


        final TextField contactPhone = new TextField("Phone", "phone", 150);
        contactPhone.setAllowBlank(false);
        contactPhone.setMaxLength(12);
        contactPhone.setMinLength(12);
        contactPhone.setRegex("^[2-9][0-9]{2}-[2-9][0-9]{2}-[0-9]{4}$");
        contactPhone.setRegexText("Phone number must be in the format xxx-xxx-xxxx.");
        formPanel.add(contactPhone);

        final TextField contactEmail = new TextField("E-Mail Address", "email", 300);
        contactEmail.setAllowBlank(false);
        contactEmail.setMaxLength(30);
        contactEmail.setMinLength(5);
        contactEmail.setRegex("^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}$");
        contactEmail.setRegexText("Email must be in the format name@domain.com.");
        formPanel.add(contactEmail);

        formPanel.add(spacer);

        final TextField username = new TextField("Username", "username", 175);
        username.setAllowBlank(false);
        username.setMaxLength(20);
        username.setMinLength(3);
        username.setRegex("^[A-Za-z][A-Za-z0-9_-]+$");
        username.setRegexText("Username must start with a letter and contain only letters, numbers, underscores, or hyphens.");
        formPanel.add(username);

        final TextField password1 = new TextField("Password", "password1", 175);
        password1.setAllowBlank(false);
        password1.setMaxLength(30);
        password1.setMinLength(6);
        password1.setPassword(true);
        formPanel.add(password1);

        final TextField password2 = new TextField("Verify Password", "password2", 175);
        password2.setAllowBlank(false);
        password2.setMaxLength(30);
        password2.setMinLength(6);
        password2.setPassword(true);
        formPanel.add(password2);

        final Label errorLabel = new Label("");
        formPanel.add(errorLabel);
        ButtonListenerAdapter baCreate =  new ButtonListenerAdapter() {
            public void onClick(Button button, EventObject eventObject) {
                String sFirmName = firmName.getText();
                String scontactFirstName = contactFirstName.getText();
                String scontactLastName = contactLastName.getText();
                String sfirmAddress1 = firmAddress1.getText();
                String sfirmAddress2 = firmAddress2.getText();
                String sfirmCity = firmCity.getText();
                String sfirmState = state.getText();
                String sfirmZip = firmZip.getText();
                String scontactPhone = contactPhone.getText();
                String scontactEmail = contactEmail.getText();
                String susername = username.getText();
                String spassword1 = password1.getText();
                String spassword2 = password2.getText();
                if (!formPanel.getForm().isValid()) {
                    errorLabel.setText("Your input is not valid. Please correct before submitting.");
                }
                else if (spassword1.equals(spassword2)) {
                    GWTUser myUser = new GWTUser();
                    GWTLawFirm myLawFirm = new GWTLawFirm();
                    myUser.setUsername(susername);
                    myUser.setPassword(spassword1);
                    myLawFirm.setUser(myUser);
                    myLawFirm.setLawFirmName(sFirmName);
                    myLawFirm.setContactFirstName(scontactFirstName);
                    myLawFirm.setContactLastName(scontactLastName);
                    myLawFirm.setFirmAddress1(sfirmAddress1);
                    myLawFirm.setFirmAddress2(sfirmAddress2);
                    myLawFirm.setEmail(scontactEmail);
                    myLawFirm.setFirmCity(sfirmCity);
                    myLawFirm.setFirmState(sfirmState);
                    myLawFirm.setFirmZip(sfirmZip);
                    myLawFirm.setContactPhone(scontactPhone);

                    AsyncCallback callback = new AsyncCallback() {
                        public void onSuccess(Object result) {
                            MsgObjBundle res = (MsgObjBundle) result;
                            if (res.getMsg().equals("USEREXISTS")) {
                                errorLabel.setText("Username already exists. Please choose another.");
                            } else if (res.getMsg().equals("FAIL")) {
                                errorLabel.setText("Unknown error occured.");
                            } else {
                                pd.goToModule("homeportal.Ace3LPortal");
                            }
                        }

                        public void onFailure(Throwable caught) {
                            errorLabel.setText("Creation Failed");
                        }
                    };
                    UserServiceAsync userService = UserService.App.getInstance();
                    userService.createLawFirm(myLawFirm, callback);
                } else {
                    errorLabel.setText("Passwords do not match.");
                }

            }
        };
        ButtonListenerAdapter baCancel = new ButtonListenerAdapter() {
            public void onClick(Button button, EventObject eventObject) {
                pd.goToModule("homeportal.Ace3LPortal");
            }
        };
        Button create = new Button("Create Law Firm", baCreate);
        Button cancel = new Button("Cancel", baCancel);
        formPanel.add(create);
        formPanel.add(cancel);
        panel.add(formPanel);
        initWidget(panel);
    }
}
